package com.heima.article.controller.v1;

import com.heima.api.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Title: TODO
 * @Description: TODO
 * @author: 丁丁
 * @date: 2021/3/12
 * @version:
 */
@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {
    @Autowired
    private AuthorService authorService;

    /**
     * 根据用户id查询作者信息
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findByUserId/{id}")
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {
        return authorService.findByUserId(id);
    }


    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        return authorService.insert(apAuthor);
    }

    /**
     * 根据名称查询作者信息
     *
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public ApAuthor selectAuthorByName(@PathVariable("name") String name) {
        return authorService.selectAuthorByName(name);
    }
}
